package com.ruoyi.manage.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.manage.domain.ServerCity;
import com.ruoyi.manage.domain.ServiceAreaItem;
import com.ruoyi.manage.domain.ServiceType;
import com.ruoyi.manage.mapper.ServiceAreaItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.manage.mapper.ServiceAreaMapper;
import com.ruoyi.manage.domain.ServiceArea;
import com.ruoyi.manage.service.IServiceAreaService;

/**
 * 区域管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-24
 */
@Service
public class ServiceAreaServiceImpl implements IServiceAreaService 
{
    @Autowired
    private ServiceAreaMapper serviceAreaMapper;
    @Autowired
    ServiceAreaItemMapper serviceAreaItemMapper;

    /**
     * 查询区域管理
     * 
     * @param id 区域管理主键
     * @return 区域管理
     */
    @Override
    public ServiceArea selectServiceAreaById(Long id)
    {
        return serviceAreaMapper.selectServiceAreaById(id);
    }

    /**
     * 查询区域管理列表
     * 
     * @param serviceArea 区域管理
     * @return 区域管理
     */
    @Override
    public List<ServiceArea> selectServiceAreaList(ServiceArea serviceArea)
    {
        return serviceAreaMapper.selectServiceAreaList(serviceArea);
    }

    /**
     * 新增区域管理
     * 
     * @param serviceArea 区域管理
     * @return 结果
     */
    @Override
    public int insertServiceArea(ServiceArea serviceArea)
    {
        return serviceAreaMapper.insertServiceArea(serviceArea);
    }

    /**
     * 修改区域管理
     * 
     * @param serviceArea 区域管理
     * @return 结果
     */
    @Override
    public int updateServiceArea(ServiceArea serviceArea)
    {
        serviceArea.setUpdateTime(DateUtils.getNowDate());
        return serviceAreaMapper.updateServiceArea(serviceArea);
    }

    /**
     * 批量删除区域管理
     * 
     * @param ids 需要删除的区域管理主键
     * @return 结果
     */
    @Override
    public int deleteServiceAreaByIds(Long[] ids)
    {
        return serviceAreaMapper.deleteServiceAreaByIds(ids);
    }

    /**
     * 删除区域管理信息
     * 
     * @param id 区域管理主键
     * @return 结果
     */
    @Override
    public int deleteServiceAreaById(Long id)
    {
        return serviceAreaMapper.deleteServiceAreaById(id);
    }

    @Override
    public int updateEnabledState(Long id, Long status) {
        if (status==2){
            ServiceAreaItem serviceAreaItem = new ServiceAreaItem();
            serviceAreaItem.setServiceAreaId(Math.toIntExact(id));
            List<ServiceAreaItem> serviceAreaItems = serviceAreaItemMapper.selectServiceAreaItemList(serviceAreaItem);
            for (ServiceAreaItem serviceAreaItem1 : serviceAreaItems) {
                if (serviceAreaItem1.getStatus()!=2){
                    return 0;
                }
            }
        }
        return serviceAreaMapper.updateEnabledState(id,status);
    }

    @Override
    public List<ServerCity> serviceTypeList() {
        return serviceAreaMapper.serviceTypeList();
    }


}
